﻿using CentaLine.Infrastructure;
using ETA.Dtos;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETA.Service
{
    public class WorkTypeService : IWorkTypeService
    {
        #region 属性

        private IWorkTypeRepository _IWorkTypeRepository { set; get; }

        #endregion

        #region 构造函数

        public WorkTypeService(IWorkTypeRepository IWorkTypeRepository)
        {
            _IWorkTypeRepository = IWorkTypeRepository;
        }

        #endregion

        public QueryWorkTypeResponse QueryWorkTypeList(QueryWorkTypeRequest req)
        {
            var key = "KeyWorkTypeList";
            if (!string.IsNullOrEmpty(req.WorkTypeId))
            {
                key += req.WorkTypeId;
            }
            if (CacheHelper.Get(key) != null)
            {
                return CacheHelper.Get(key) as QueryWorkTypeResponse;
            }

            var response = new QueryWorkTypeResponse();
            try
            {
                var workTypeList = new List<WorkType>();
                if (!string.IsNullOrEmpty(req.WorkTypeId))
                {
                    workTypeList = _IWorkTypeRepository.GetAllList(b => b.WorkTypeId == req.WorkTypeId);
                    response.Result.IsSuccess = true;
                }
                else
                {
                    workTypeList = _IWorkTypeRepository.GetAllList();
                    response.Result.IsSuccess = true;
                }
                response.WorkTypeList = AutoMapper.Mapper.Map<List<WorkType>, List<WorkTypeDto>>(workTypeList);
                if (response.Result.IsSuccess)
                {
                    CacheHelper.AddHours(key, response, 20);
                }
            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
            }
            return response;
        }
    }
}
